﻿using System;
using System.Threading.Tasks;
using System.Threading;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;

namespace Remember
{
    public class TrayManager : ObjectBase
    {
        private string trayText;
        private Views.MainWindow MainWindow;
        private MainWindowViewModel MainModel;

        private static List<Note> upNotes;

        public TrayManager(MainWindowViewModel mainModel, Views.MainWindow mainWindow)
        {
            MainWindow = mainWindow;
            MainModel = mainModel;
            upNotes = mainModel.UpcomingNotes;

            Task NotesTraker = new Task(() => Trak());
            NotesTraker.Start();
        }

        public string TrayText
        {
            get { return trayText; }
            set
            {
                if (trayText == value)
                    return;
                trayText = value;
                OnPropertyChanged("TrayText");
            }
        }

        public void ShowBalloon(string text)
        {
            MainWindow.TrayIcon.ShowBalloonTip("Remember", text, Hardcodet.Wpf.TaskbarNotification.BalloonIcon.Info);
        }

        private void Trak()
        {
            var notes = MainModel.UpcomingNotes.Where(n => IsClose(n));
            while(true)
            {
                if(notes.Count() > 0)
                {
                    ShowBalloon("There are notes for you today!");
                }
                Thread.Sleep(1000);
            }
        }

        private bool IsClose(Note note)
        {
            if( note.ExpiryDate.Value.Year    == DateTime.Now.Year &&
                note.ExpiryDate.Value.Month   == DateTime.Now.Month &&
                note.ExpiryDate.Value.Day     == DateTime.Now.Day)
                return true;

            return false;
        }
    }
}
